package com.gibs.peixunapi.controller;

import com.gibs.peixunapi.VO.CreateSubOptionVO;
import com.gibs.peixunapi.VO.SubOptionVO;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.excepiton.BaseException;
import com.gibs.peixunapi.model.SubOption;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.SubOptionService;
import com.gibs.peixunapi.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

//import com.gibs.peixunapi.config.ConfigUtils;
//import com.gibs.peixunapi.controller.Application;

/**
 * 功能：【题库选项】的后台控制器
 * 项目：装配式培训平台
 * 日期：2020-08-24
 * 作者：梁嘉伟
 *
 * @author liangjiawei
 */

@Slf4j
@RestController
@RequestMapping("/SubOptions")
@Api(tags = "选项api")
public class SubOptions extends Application {

    @Autowired
    private SubOptionService subOptionService;

    @PostMapping("/create")
    @ApiOperation(value = "新增选项")
    public Result create(@RequestBody CreateSubOptionVO createSubOptionVO) {
        if (createSubOptionVO.getSubOptionVOList().isEmpty() || createSubOptionVO.getSubjectHubId() == null) {
            throw new BaseException(ResultEnum.PARAM_ERROR);
        }
        return subOptionService.create(createSubOptionVO.getSubOptionVOList(), createSubOptionVO.getSubjectHubId());
    }

    @Transactional
    @PostMapping("/save")
    public Result save(@RequestBody CreateSubOptionVO createSubOptionVO) {
        if (createSubOptionVO.getSubOptionVOList().isEmpty() || createSubOptionVO.getSubjectHubId() == null) {
            throw new BaseException(ResultEnum.PARAM_ERROR);
        }
        return subOptionService.save(createSubOptionVO.getSubOptionVOList());

    }

    @PostMapping("/delete")
    public Result delete(Integer id) {
        if (id == null) {
            throw new BaseException(ResultEnum.PARAM_ERROR);
        }
        return subOptionService.delete(id);

    }

    @GetMapping("/get")
    public Result get(Integer id) {
        return subOptionService.get(id);
    }

    @GetMapping("/getList")
    public Result getList(String key) {
        return subOptionService.getList(Integer.parseInt(key));
    }


}

